package org.example.managere.controller.feibiao;

import org.example.managere.dto.feibiao.AmArchivesOperationTestDto;
import org.example.managere.service.feibiao.AmArchivesOperationTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 电机性能试验数据控制器
 */
@RestController
@RequestMapping("/api/feibiao/operation-test")
public class AmArchivesOperationTestController {

    @Autowired
    private AmArchivesOperationTestService operationTestService;

    /**
     * 获取所有性能试验数据（分页）
     */
    @GetMapping
    public ResponseEntity<Page<AmArchivesOperationTestDto>> findAll(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "testId,asc") String sort) {
        String[] sortParams = sort.split(",");
        Sort.Direction direction = sortParams.length > 1 && sortParams[1].equalsIgnoreCase("desc") 
            ? Sort.Direction.DESC 
            : Sort.Direction.ASC;
        PageRequest pageRequest = PageRequest.of(page, size, Sort.by(direction, sortParams[0]));
        return ResponseEntity.ok(operationTestService.findAll(pageRequest));
    }

    /**
     * 获取所有性能试验数据
     */
    @GetMapping("/all")
    public ResponseEntity<List<AmArchivesOperationTestDto>> findAll() {
        return ResponseEntity.ok(operationTestService.findAll());
    }

    /**
     * 根据ID获取性能试验数据
     */
    @GetMapping("/{id}")
    public ResponseEntity<AmArchivesOperationTestDto> findById(@PathVariable Integer id) {
        return operationTestService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 创建性能试验数据
     */
    @PostMapping
    public ResponseEntity<AmArchivesOperationTestDto> create(@RequestBody AmArchivesOperationTestDto operationTestDto) {
        return ResponseEntity.ok(operationTestService.save(operationTestDto));
    }

    /**
     * 更新性能试验数据
     */
    @PutMapping("/{id}")
    public ResponseEntity<AmArchivesOperationTestDto> update(
            @PathVariable Integer id,
            @RequestBody AmArchivesOperationTestDto operationTestDto) {
        operationTestDto.setTestId(id);
        return ResponseEntity.ok(operationTestService.update(operationTestDto));
    }

    /**
     * 删除性能试验数据
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        operationTestService.deleteById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据电机ID查询性能试验数据
     */
    @GetMapping("/by-motor")
    public ResponseEntity<List<AmArchivesOperationTestDto>> findByMotorId(@RequestParam String motorId) {
        return ResponseEntity.ok(operationTestService.findByMotorId(motorId));
    }

    /**
     * 根据试验类型查询性能试验数据
     */
    @GetMapping("/by-test-type")
    public ResponseEntity<List<AmArchivesOperationTestDto>> findByTestType(@RequestParam String testType) {
        return ResponseEntity.ok(operationTestService.findByTestType(testType));
    }

    /**
     * 根据日期范围查询性能试验数据
     */
    @GetMapping("/by-date-range")
    public ResponseEntity<List<AmArchivesOperationTestDto>> findByDateRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        return ResponseEntity.ok(operationTestService.findByTestTimeBetween(startDate, endDate));
    }

    /**
     * 获取电机的最新性能试验数据
     */
    @GetMapping("/latest")
    public ResponseEntity<List<AmArchivesOperationTestDto>> getLatestTestsByMotorId(
            @RequestParam String motorId,
            @RequestParam(defaultValue = "1") int limit) {
        return ResponseEntity.ok(operationTestService.findLatestTestsByMotorId(motorId, limit));
    }
} 